<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      let tree = {
        val: 1,
        left: {
          val: 2,
          left: {
            val: 4
          },
          right: {
            val: 5
          }
        },
        right: {
          val: 3,
          left: {
            val: 6
          }
        }
      }
      function bfsOutNull(root) {
        if (!root) return []
        let queue = [root]
        let res = []
        while (queue.length) {
          res.push([])
          let len = queue.length
          for (let i = 0; i < len; i++) {
            let node = queue.shift()
            res[res.length - 1].push(node.val)
            if (node.left) {
              queue.push(node.left)
            }
            if (node.right) {
              queue.push(node.right)
            }
          }
        }
        console.log(res)
      }

      function bfsOutNull(root) {
        if (!root) return []
        let queue = [root]
        let res = []
        while (queue.length) {
          let len = queue.length
          res.push([])
          for (let i = 0; i < len; i++) {
            let node = queue.shift()
            if (!node) {
              res[res.length - 1].push(null)
            } else {
              res[res.length - 1].push(node.val)
              queue.push(node.left)
              queue.push(node.right)
            }
          }
        }
        console.log(res)
      }
      bfsOutNull(tree)
    </script>
  </body>
</html>
